package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.ApprovalModify;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.service.TemplateServicePrx;
import com.mp.user.entity.User;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getModifyList")
public class GetModifyList extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(GetModifyList.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);
            String reqBody = getContent(req);
            logger.info("获取审批历史记录:{}", reqBody);
            Query query = JSONObject.parseObject(reqBody, Query.class);
            query.initUser(user);

            TemplateServicePrx templateServicePrx = ServantUtils.getObject(TemplateServicePrx.class);
            List<ApprovalModify> list = templateServicePrx.getModifyList(query);

            returnResponse(resp, list);
        } catch (Throwable throwable) {
            onThrowable(req, resp, throwable);
        }
    }

    private void returnResponse(HttpServletResponse resp, List<ApprovalModify> list) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("erpCode", "000");
        jsonObject.put("erpMsg", "获得成功");
        jsonObject.put("item", list);

        logger.info(jsonObject.toJSONString());

        printJson(resp, jsonObject.toJSONString());
    }
}
